<?php

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/question/type/preg/preg_fa.php');
require_once($CFG->dirroot . '/question/type/preg/nfa_matcher/nfa_nodes.php');

class qtype_preg_fa_intersect_fa_big_test extends PHPUnit_Framework_TestCase {

    public function test_big_forward() {
        $dotdescription1 = 'digraph example
                            {
                                0;
                                9;
                                0->1[label="[a-c]"];
                                1->2[label="[ab]"];
                                1->3[label="[ab]"];
                                2->8[label="[as]"];
                                1->5[label="[a-z]"];
                                3->4[label="[a-c]"];
                                4->8[label="[ab]"];
                                5->6[label="[^a]"];
                                7->8[label="[a-c]"];
                                6->7[label="[a-c]"];
                                8->9[label="[ab]"];
                                8->1[label="[ab]"];
                                9->2[label="[ab]"];
                            }';
        $dotdescription2 = 'digraph example
                            {
                                0;
                                9;
                                0->1[label="[a-z]"];
                                1->2[label="[ab]"];
                                1->3[label="[as]"];
                                2->6[label="[a-c]"];
                                3->4[label="[.]"];
                                4->5[label="[a-d]"];
                                5->6[label="[as01]"];
                                6->7[label="[ax-y]"];
                                6->8[label="[.]"];
                                7->9[label="[a]"];
                                8->9[label="[a-c]"];
                                7->2[label="[a-z]"];
                                8->1[label="[a-f]"];
                                9->9[label="[b-n]"];
                            }';
        $dotresult = 'digraph res 
                    {
                        "0,";
                        "9,";
                        "0,"->"1,"[label="[a-c]",color=violet];
                        "1,"->"5,0"[label="[a-z]",color=violet];
                        "1,"->"2,"[label="[ab]",color=violet];
                        "1,"->"3,"[label="[ab]",color=violet];
                        "1,"->"5,"[label="[a-z]",color=violet];
                        "2,"->"8,"[label="[as]",color=violet];
                        "3,"->"4,"[label="[a-c]",color=violet];
                        "5,0"->"6,1"[label="[^a]",color=red];
                        "8,"->"9,"[label="[ab]",color=violet];
                        "8,"->"9,"[label="[ab]",color=violet];
                        "8,"->"1,"[label="[ab]",color=violet];
                        "4,"->"8,"[label="[ab]",color=violet];
                        "9,"->"2,"[label="[ab]",color=violet];
                        "6,1"->"7,2"[label="[ab]",color=red];
                        "6,1"->"7,3"[label="[a]",color=red];
                        "7,2"->"8,6"[label="[a-c]",color=red];
                        "7,3"->"8,4"[label="[a-c]",color=red];
                        "8,6"->"9,7"[label="[a]",color=red];
                        "8,6"->"9,8"[label="[ab]",color=red];
                        "8,6"->"1,7"[label="[a]",color=red];
                        "8,6"->"1,8"[label="[ab]",color=red];
                        "8,4"->"9,5"[label="[ab]",color=red];
                        "8,4"->"1,5"[label="[ab]",color=red];
                        "9,7"->"2,9"[label="[a]",color=red];
                        "9,7"->"2,2"[label="[ab]",color=red];
                        "9,8"->"2,9"[label="[ab]",color=red];
                        "9,8"->"2,1"[label="[ab]",color=red];
                        "1,7"->"2,9"[label="[a]",color=red];
                        "1,7"->"2,2"[label="[ab]",color=red];
                        "1,7"->"3,9"[label="[a]",color=red];
                        "1,7"->"3,2"[label="[ab]",color=red];
                        "1,7"->"5,9"[label="[a]",color=red];
                        "1,7"->"5,2"[label="[a-z]",color=red];
                        "1,8"->"2,9"[label="[ab]",color=red];
                        "1,8"->"2,1"[label="[ab]",color=red];
                        "1,8"->"3,9"[label="[ab]",color=red];
                        "1,8"->"3,1"[label="[ab]",color=red];
                        "1,8"->"5,9"[label="[a-c]",color=red];
                        "1,8"->"5,1"[label="[a-f]",color=red];
                        "9,5"->"2,6"[label="[a]",color=red];
                        "1,5"->"2,6"[label="[a]",color=red];
                        "1,5"->"3,6"[label="[a]",color=red];
                        "1,5"->"5,6"[label="[as]",color=red];
                        "2,9"->"8,"[label="[as]",color=violet];
                        "2,2"->"8,6"[label="[a]",color=red];
                        "2,1"->"8,2"[label="[a]",color=red];
                        "2,1"->"8,3"[label="[as]",color=red];
                        "3,9"->"4,9"[label="[b-c]",color=red];
                        "3,2"->"4,6"[label="[a-c]",color=red];
                        "5,9"->"6,"[label="[^a]",color=violet];
                        "5,2"->"6,6"[label="[^a]",color=red];
                        "3,1"->"4,2"[label="[ab]",color=red];
                        "3,1"->"4,3"[label="[a]",color=red];
                        "5,1"->"6,2"[label="[^a]",color=red];
                        "5,1"->"6,3"[label="[^a]",color=red];
                        "2,6"->"8,7"[label="[a]",color=red];
                        "2,6"->"8,8"[label="[as]",color=red];
                        "3,6"->"4,7"[label="[a]",color=red];
                        "3,6"->"4,8"[label="[a-c]",color=red];
                        "5,6"->"6,7"[label="[^a]",color=red];
                        "5,6"->"6,8"[label="[^a]",color=red];
                        "8,2"->"9,6"[label="[ab]",color=red];
                        "8,2"->"1,6"[label="[ab]",color=red];
                        "8,3"->"1,4"[label="[ab]",color=red];
                        "4,9"->"8,9"[label="[b]",color=red];
                        "4,6"->"8,7"[label="[a]",color=red];
                        "4,6"->"8,8"[label="[ab]",color=red];
                        "6,6"->"7,7"[label="[a]",color=red];
                        "6,6"->"7,8"[label="[a-c]",color=red];
                        "4,2"->"8,6"[label="[ab]",color=red];
                        "4,3"->"8,4"[label="[ab]",color=red];
                        "6,2"->"7,6"[label="[a-c]",color=red];
                        "6,3"->"7,4"[label="[a-c]",color=red];
                        "8,7"->"9,9"[label="[a]",color=red];
                        "8,7"->"9,2"[label="[ab]",color=red];
                        "8,7"->"1,9"[label="[a]",color=red];
                        "8,7"->"1,2"[label="[ab]",color=red];
                        "8,8"->"9,9"[label="[ab]",color=red];
                        "8,8"->"9,1"[label="[ab]",color=red];
                        "8,8"->"1,9"[label="[ab]",color=red];
                        "8,8"->"1,1"[label="[ab]",color=red];
                        "4,7"->"8,9"[label="[a]",color=red];
                        "4,7"->"8,2"[label="[ab]",color=red];
                        "4,8"->"8,9"[label="[ab]",color=red];
                        "4,8"->"8,1"[label="[ab]",color=red];
                        "6,7"->"7,9"[label="[a]",color=red];
                        "6,7"->"7,2"[label="[a-c]",color=red];
                        "6,8"->"7,9"[label="[a-c]",color=red];
                        "6,8"->"7,1"[label="[a-c]",color=red];
                        "9,6"->"2,7"[label="[a]",color=red];
                        "9,6"->"2,8"[label="[ab]",color=red];
                        "1,6"->"2,7"[label="[a]",color=red];
                        "1,6"->"2,8"[label="[ab]",color=red];
                        "1,6"->"3,7"[label="[a]",color=red];
                        "1,6"->"3,8"[label="[ab]",color=red];
                        "1,6"->"5,7"[label="[ax-y]",color=red];
                        "1,6"->"5,8"[label="[a-z]",color=red];
                        "9,4"->"2,5"[label="[ab]",color=red];
                        "1,4"->"2,5"[label="[ab]",color=red];
                        "1,4"->"3,5"[label="[ab]",color=red];
                        "1,4"->"5,5"[label="[a-d]",color=red];
                        "8,9"->"9,9"[label="[b]",color=red];
                        "8,9"->"1,9"[label="[b]",color=red];
                        "7,7"->"8,9"[label="[a]",color=red];
                        "7,7"->"8,2"[label="[a-c]",color=red];
                        "7,8"->"8,9"[label="[a-c]",color=red];
                        "7,8"->"8,1"[label="[a-c]",color=red];
                        "7,6"->"8,7"[label="[a]",color=red];
                        "7,6"->"8,8"[label="[a-c]",color=red];
                        "7,4"->"8,5"[label="[a-c]",color=red];
                        "9,9"->"2,9"[label="[b]",color=red];
                        "9,2"->"2,6"[label="[ab]",color=red];
                        "1,9"->"2,9"[label="[b]",color=red];
                        "1,9"->"3,9"[label="[b]",color=red];
                        "1,9"->"5,9"[label="[b-n]",color=red];
                        "1,2"->"2,6"[label="[ab]",color=red];
                        "1,2"->"3,6"[label="[ab]",color=red];
                        "1,2"->"5,6"[label="[a-c]",color=red];
                        "9,1"->"2,2"[label="[ab]",color=red];
                        "9,1"->"2,3"[label="[a]",color=red];
                        "1,1"->"2,2"[label="[ab]",color=red];
                        "1,1"->"2,3"[label="[a]",color=red];
                        "1,1"->"3,2"[label="[ab]",color=red];
                        "1,1"->"3,3"[label="[a]",color=red];
                        "1,1"->"5,2"[label="[ab]",color=red];
                        "1,1"->"5,3"[label="[as]",color=red];
                        "8,1"->"9,2"[label="[ab]",color=red];
                        "8,1"->"9,3"[label="[a]",color=red];
                        "8,1"->"1,2"[label="[ab]",color=red];
                        "8,1"->"1,3"[label="[a]",color=red];
                        "7,9"->"8,9"[label="[b-c]",color=red];
                        "7,1"->"8,2"[label="[ab]",color=red];
                        "7,1"->"8,3"[label="[a]",color=red];
                        "2,7"->"8,9"[label="[a]",color=red];
                        "2,7"->"8,2"[label="[as]",color=red];
                        "2,8"->"8,9"[label="[a]",color=red];
                        "2,8"->"8,1"[label="[a]",color=red];
                        "3,7"->"4,9"[label="[a]",color=red];
                        "3,7"->"4,2"[label="[a-c]",color=red];
                        "3,8"->"4,9"[label="[a-c]",color=red];
                        "3,8"->"4,1"[label="[a-c]",color=red];
                        "5,7"->"6,9"[label="[^a]",color=red];
                        "5,7"->"6,2"[label="[^a]",color=red];
                        "5,8"->"6,9"[label="[^a]",color=red];
                        "5,8"->"6,1"[label="[^a]",color=red];
                        "2,5"->"8,6"[label="[as]",color=red];
                        "3,5"->"4,6"[label="[a]",color=red];
                        "5,5"->"6,6"[label="[^a]",color=red];
                        "8,5"->"9,6"[label="[a]",color=red];
                        "8,5"->"1,6"[label="[a]",color=red];
                        "2,3"->"8,4"[label="[as]",color=red];
                        "3,3"->"4,4"[label="[a-c]",color=red];
                        "5,3"->"6,4"[label="[^a]",color=red];
                        "9,3"->"2,4"[label="[ab]",color=red];
                        "1,3"->"2,4"[label="[ab]",color=red];
                        "1,3"->"3,4"[label="[ab]",color=red];
                        "1,3"->"5,4"[label="[a-z]",color=red];
                        "4,1"->"8,2"[label="[ab]",color=red];
                        "4,1"->"8,3"[label="[a]",color=red];
                        "6,9"->"7,9"[label="[b-c]",color=red];
                        "4,4"->"8,5"[label="[ab]",color=red];
                        "6,4"->"7,5"[label="[a-c]",color=red];
                        "2,4"->"8,5"[label="[a]",color=red];
                        "3,4"->"4,5"[label="[a-c]",color=red];
                        "5,4"->"6,5"[label="[^a]",color=red];
                        "7,5"->"8,6"[label="[a]",color=red];
                        "4,5"->"8,6"[label="[a]",color=red];
                        "6,5"->"7,6"[label="[a]",color=red];
                        "5,"->"6,"[label="[^a]",color=violet];
                        "6,"->"7,"[label="[a-c]",color=violet];
                        "7,"->"8,"[label="[a-c]",color=violet];
                    }';

        $search = '
                    ';
        $replace = '\n';
        $origin = qtype_preg_fa_transition::ORIGIN_TRANSITION_SECOND;

        $firstautomata = new qtype_preg_nfa(0, 0, 0, array());
        $firstautomata->read_fa($dotdescription1);
        $secondautomata = new qtype_preg_nfa(0, 0, 0, array());
        $secondautomata->read_fa($dotdescription2, $origin);
        $resultautomata = new qtype_preg_nfa(0, 0, 0, array());

        $resultautomata = $firstautomata->intersect_fa($secondautomata, 5, 0);
        $result = $resultautomata->write_fa();
        $dotresult = str_replace($search, $replace, $dotresult);
        $this->assertEquals($dotresult, $result, 'Result automata is not equal to expected');
    }

    public function test_big_back() {
        $dotdescription1 = 'digraph example
                            {
                                0;
                                9;
                                0->1[label="[a-c]"];
                                1->2[label="[ab]"];
                                1->3[label="[ab]"];
                                2->8[label="[as]"];
                                1->5[label="[a-z]"];
                                3->4[label="[a-c]"];
                                4->8[label="[ab]"];
                                5->6[label="[^a]"];
                                7->8[label="[a-c]"];
                                6->7[label="[a-c]"];
                                8->9[label="[ab]"];
                                8->1[label="[ab]"];
                                9->2[label="[ab]"];
                            }';
        $dotdescription2 = 'digraph example
                            {
                                0;
                                9;
                                0->1[label="[a-z]"];
                                1->2[label="[ab]"];
                                1->3[label="[as]"];
                                2->6[label="[a-c]"];
                                3->4[label="[.]"];
                                4->5[label="[a-d]"];
                                5->6[label="[as01]"];
                                6->7[label="[ax-y]"];
                                6->8[label="[.]"];
                                7->9[label="[a]"];
                                8->9[label="[a-c]"];
                                7->2[label="[a-z]"];
                                8->1[label="[a-f]"];
                                9->9[label="[b-n]"];
                            }';
        $dotresult = 'digraph res 
                    {
                        "0,";",0";
                        "9,";
                        "9,"->"2,"[label="[ab]",color=violet];
                        "9,"->"2,0"[label="[ab]",color=violet];
                        "8,"->"1,"[label="[ab]",color=violet];
                        "8,"->"1,0"[label="[ab]",color=violet];
                        "8,"->"9,"[label="[ab]",color=violet];
                        "8,"->"1,"[label="[ab]",color=violet];
                        "8,"->"9,0"[label="[ab]",color=violet];
                        "2,"->"8,"[label="[as]",color=violet];
                        "2,"->"8,0"[label="[as]",color=violet];
                        "4,"->"8,"[label="[ab]",color=violet];
                        "4,"->"8,0"[label="[ab]",color=violet];
                        "7,"->"8,"[label="[a-c]",color=violet];
                        "7,"->"8,0"[label="[a-c]",color=violet];
                        "1,"->"5,0"[label="[a-z]",color=violet];
                        "1,"->"2,"[label="[ab]",color=violet];
                        "1,"->"2,0"[label="[ab]",color=violet];
                        "1,"->"2,"[label="[ab]",color=violet];
                        "1,"->"3,"[label="[ab]",color=violet];
                        "1,"->"5,"[label="[a-z]",color=violet];
                        "1,"->"3,0"[label="[ab]",color=violet];
                        "3,"->"4,"[label="[a-c]",color=violet];
                        "3,"->"4,0"[label="[a-c]",color=violet];
                        "6,"->"7,"[label="[a-c]",color=violet];
                        "6,"->"7,0"[label="[a-c]",color=violet];
                        "0,"->"1,"[label="[a-c]",color=violet];
                        "0,"->"1,0"[label="[a-c]",color=violet];
                        "5,9"->"6,"[label="[^a]",color=violet];
                        "1,7"->"5,9"[label="[a]",color=red];
                        "1,7"->"2,2"[label="[ab]",color=red];
                        "1,7"->"3,2"[label="[ab]",color=red];
                        "1,7"->"3,9"[label="[a]",color=red];
                        "1,7"->"5,2"[label="[a-z]",color=red];
                        "1,8"->"5,9"[label="[a-c]",color=red];
                        "1,8"->"3,1"[label="[ab]",color=red];
                        "1,8"->"3,9"[label="[ab]",color=red];
                        "1,8"->"5,1"[label="[a-f]",color=red];
                        "1,8"->"2,1"[label="[ab]",color=red];
                        "1,9"->"5,9"[label="[b-n]",color=red];
                        "1,9"->"3,9"[label="[b]",color=red];
                        "0,6"->"1,7"[label="[a]",color=red];
                        "0,6"->"1,8"[label="[a-c]",color=red];
                        "8,6"->"1,7"[label="[a]",color=red];
                        "8,6"->"1,8"[label="[ab]",color=red];
                        "8,6"->"9,7"[label="[a]",color=red];
                        "8,6"->"9,8"[label="[ab]",color=red];
                        "0,7"->"1,9"[label="[a]",color=red];
                        "0,7"->"1,2"[label="[a-c]",color=red];
                        "0,8"->"1,9"[label="[a-c]",color=red];
                        "0,8"->"1,1"[label="[a-c]",color=red];
                        "0,9"->"1,9"[label="[b-c]",color=red];
                        "8,7"->"1,9"[label="[a]",color=red];
                        "8,7"->"1,2"[label="[ab]",color=red];
                        "8,7"->"9,2"[label="[ab]",color=red];
                        "8,8"->"1,9"[label="[ab]",color=red];
                        "8,8"->"1,1"[label="[ab]",color=red];
                        "8,8"->"9,1"[label="[ab]",color=red];
                        "8,9"->"1,9"[label="[b]",color=red];
                        "2,2"->"8,6"[label="[a]",color=red];
                        "2,5"->"8,6"[label="[as]",color=red];
                        "4,2"->"8,6"[label="[ab]",color=red];
                        "4,5"->"8,6"[label="[a]",color=red];
                        "7,2"->"8,6"[label="[a-c]",color=red];
                        "7,5"->"8,6"[label="[a]",color=red];
                        "2,6"->"8,7"[label="[a]",color=red];
                        "2,6"->"8,8"[label="[as]",color=red];
                        "4,6"->"8,7"[label="[a]",color=red];
                        "4,6"->"8,8"[label="[ab]",color=red];
                        "7,6"->"8,7"[label="[a]",color=red];
                        "7,6"->"8,8"[label="[a-c]",color=red];
                        "2,7"->"8,9"[label="[a]",color=red];
                        "2,7"->"8,2"[label="[as]",color=red];
                        "2,8"->"8,9"[label="[a]",color=red];
                        "2,8"->"8,1"[label="[a]",color=red];
                        "4,7"->"8,9"[label="[a]",color=red];
                        "4,7"->"8,2"[label="[ab]",color=red];
                        "4,8"->"8,9"[label="[ab]",color=red];
                        "4,8"->"8,1"[label="[ab]",color=red];
                        "4,9"->"8,9"[label="[b]",color=red];
                        "7,7"->"8,9"[label="[a]",color=red];
                        "7,7"->"8,2"[label="[a-c]",color=red];
                        "7,8"->"8,9"[label="[a-c]",color=red];
                        "7,8"->"8,1"[label="[a-c]",color=red];
                        "7,9"->"8,9"[label="[b-c]",color=red];
                        "1,1"->"2,2"[label="[ab]",color=red];
                        "1,1"->"3,2"[label="[ab]",color=red];
                        "1,1"->"5,3"[label="[as]",color=red];
                        "1,1"->"5,2"[label="[ab]",color=red];
                        "1,1"->"2,3"[label="[a]",color=red];
                        "1,1"->"3,3"[label="[a]",color=red];
                        "9,1"->"2,2"[label="[ab]",color=red];
                        "9,1"->"2,3"[label="[a]",color=red];
                        "9,7"->"2,2"[label="[ab]",color=red];
                        "1,4"->"2,5"[label="[ab]",color=red];
                        "1,4"->"3,5"[label="[ab]",color=red];
                        "1,4"->"5,5"[label="[a-d]",color=red];
                        "9,4"->"2,5"[label="[ab]",color=red];
                        "3,1"->"4,2"[label="[ab]",color=red];
                        "3,1"->"4,3"[label="[a]",color=red];
                        "3,7"->"4,2"[label="[a-c]",color=red];
                        "3,7"->"4,9"[label="[a]",color=red];
                        "3,4"->"4,5"[label="[a-c]",color=red];
                        "6,1"->"7,2"[label="[ab]",color=red];
                        "6,1"->"7,3"[label="[a]",color=red];
                        "6,7"->"7,2"[label="[a-c]",color=red];
                        "6,7"->"7,9"[label="[a]",color=red];
                        "6,4"->"7,5"[label="[a-c]",color=red];
                        "1,2"->"2,6"[label="[ab]",color=red];
                        "1,2"->"3,6"[label="[ab]",color=red];
                        "1,2"->"5,6"[label="[a-c]",color=red];
                        "1,5"->"2,6"[label="[a]",color=red];
                        "1,5"->"3,6"[label="[a]",color=red];
                        "1,5"->"5,6"[label="[as]",color=red];
                        "9,2"->"2,6"[label="[ab]",color=red];
                        "9,5"->"2,6"[label="[a]",color=red];
                        "3,2"->"4,6"[label="[a-c]",color=red];
                        "3,5"->"4,6"[label="[a]",color=red];
                        "6,2"->"7,6"[label="[a-c]",color=red];
                        "6,5"->"7,6"[label="[a]",color=red];
                        "1,6"->"2,7"[label="[a]",color=red];
                        "1,6"->"2,8"[label="[ab]",color=red];
                        "1,6"->"3,7"[label="[a]",color=red];
                        "1,6"->"3,8"[label="[ab]",color=red];
                        "1,6"->"5,8"[label="[a-z]",color=red];
                        "1,6"->"5,7"[label="[ax-y]",color=red];
                        "9,6"->"2,7"[label="[a]",color=red];
                        "9,6"->"2,8"[label="[ab]",color=red];
                        "3,6"->"4,7"[label="[a]",color=red];
                        "3,6"->"4,8"[label="[a-c]",color=red];
                        "3,8"->"4,9"[label="[a-c]",color=red];
                        "3,8"->"4,1"[label="[a-c]",color=red];
                        "3,9"->"4,9"[label="[b-c]",color=red];
                        "6,6"->"7,7"[label="[a]",color=red];
                        "6,6"->"7,8"[label="[a-c]",color=red];
                        "6,8"->"7,9"[label="[a-c]",color=red];
                        "6,8"->"7,1"[label="[a-c]",color=red];
                        "6,9"->"7,9"[label="[b-c]",color=red];
                        "0,0"->"1,1"[label="[a-c]",color=red];
                        "8,0"->"1,1"[label="[ab]",color=red];
                        "8,0"->"9,1"[label="[ab]",color=red];
                        "0,3"->"1,4"[label="[a-c]",color=red];
                        "8,3"->"1,4"[label="[ab]",color=red];
                        "8,3"->"9,4"[label="[ab]",color=red];
                        "1,0"->"3,1"[label="[ab]",color=red];
                        "1,0"->"5,1"[label="[a-z]",color=red];
                        "1,0"->"2,1"[label="[ab]",color=red];
                        "1,3"->"3,4"[label="[ab]",color=red];
                        "1,3"->"5,4"[label="[a-z]",color=red];
                        "1,3"->"2,4"[label="[ab]",color=red];
                        "5,0"->"6,1"[label="[^a]",color=red];
                        "5,8"->"6,1"[label="[^a]",color=red];
                        "5,8"->"6,9"[label="[^a]",color=red];
                        "5,6"->"6,7"[label="[^a]",color=red];
                        "5,6"->"6,8"[label="[^a]",color=red];
                        "5,3"->"6,4"[label="[^a]",color=red];
                        "0,1"->"1,2"[label="[ab]",color=red];
                        "0,1"->"1,3"[label="[a]",color=red];
                        "8,1"->"1,2"[label="[ab]",color=red];
                        "8,1"->"9,2"[label="[ab]",color=red];
                        "8,1"->"1,3"[label="[a]",color=red];
                        "8,1"->"9,3"[label="[a]",color=red];
                        "0,4"->"1,5"[label="[a-c]",color=red];
                        "8,4"->"1,5"[label="[ab]",color=red];
                        "8,4"->"9,5"[label="[ab]",color=red];
                        "5,1"->"6,2"[label="[^a]",color=red];
                        "5,1"->"6,3"[label="[^a]",color=red];
                        "5,7"->"6,2"[label="[^a]",color=red];
                        "5,7"->"6,9"[label="[^a]",color=red];
                        "5,4"->"6,5"[label="[^a]",color=red];
                        "0,2"->"1,6"[label="[a-c]",color=red];
                        "0,5"->"1,6"[label="[a]",color=red];
                        "8,2"->"1,6"[label="[ab]",color=red];
                        "8,2"->"9,6"[label="[ab]",color=red];
                        "8,5"->"1,6"[label="[a]",color=red];
                        "8,5"->"9,6"[label="[a]",color=red];
                        "5,2"->"6,6"[label="[^a]",color=red];
                        "5,5"->"6,6"[label="[^a]",color=red];
                        "2,1"->"8,3"[label="[as]",color=red];
                        "2,1"->"8,2"[label="[a]",color=red];
                        "4,1"->"8,3"[label="[a]",color=red];
                        "4,1"->"8,2"[label="[ab]",color=red];
                        "7,1"->"8,3"[label="[a]",color=red];
                        "7,1"->"8,2"[label="[ab]",color=red];
                        "2,0"->"8,1"[label="[as]",color=red];
                        "4,0"->"8,1"[label="[ab]",color=red];
                        "7,0"->"8,1"[label="[a-c]",color=red];
                        "2,3"->"8,4"[label="[as]",color=red];
                        "4,3"->"8,4"[label="[ab]",color=red];
                        "7,3"->"8,4"[label="[a-c]",color=red];
                        "2,4"->"8,5"[label="[a]",color=red];
                        "4,4"->"8,5"[label="[ab]",color=red];
                        "7,4"->"8,5"[label="[a-c]",color=red];
                        "9,0"->"2,1"[label="[ab]",color=red];
                        "9,8"->"2,1"[label="[ab]",color=red];
                        "3,0"->"4,1"[label="[a-c]",color=red];
                        "6,0"->"7,1"[label="[a-c]",color=red];
                        "9,3"->"2,4"[label="[ab]",color=red];
                        "3,3"->"4,4"[label="[a-c]",color=red];
                        "6,3"->"7,4"[label="[a-c]",color=red];
                        ",2"->",6"[label="[a-c]",color=blue];
                        ",2"->"0,6"[label="[a-c]",color=blue];
                        ",5"->",6"[label="[as01]",color=blue];
                        ",5"->"0,6"[label="[as01]",color=blue];
                        ",1"->"0,3"[label="[as]",color=blue];
                        ",1"->",2"[label="[ab]",color=blue];
                        ",1"->",3"[label="[as]",color=blue];
                        ",1"->"0,2"[label="[ab]",color=blue];
                        ",7"->"0,9"[label="[a]",color=blue];
                        ",7"->",9"[label="[a]",color=blue];
                        ",7"->",2"[label="[a-z]",color=blue];
                        ",7"->"0,2"[label="[a-z]",color=blue];
                        ",4"->",5"[label="[a-d]",color=blue];
                        ",4"->"0,5"[label="[a-d]",color=blue];
                        ",0"->",1"[label="[a-z]",color=blue];
                        ",0"->"0,1"[label="[a-z]",color=blue];
                        ",8"->"0,9"[label="[a-c]",color=blue];
                        ",8"->",9"[label="[a-c]",color=blue];
                        ",8"->",1"[label="[a-f]",color=blue];
                        ",8"->"0,1"[label="[a-f]",color=blue];
                        ",6"->"0,7"[label="[ax-y]",color=blue];
                        ",6"->",7"[label="[ax-y]",color=blue];
                        ",6"->",8"[label="[.]",color=blue];
                        ",6"->"0,8"[label="[.]",color=blue];
                        ",3"->",4"[label="[.]",color=blue];
                        ",3"->"0,4"[label="[.]",color=blue];
                        ",9"->",9"[label="[b-n]",color=blue];
                        ",9"->"0,9"[label="[b-n]",color=blue];
                        ",9"->"9,"[label="[]",color=blue];
                        "5,"->"6,"[label="[^a]",color=violet];
                        "5,"->"6,0"[label="[^a]",color=violet];
                    }';

        $search = '
                    ';
        $replace = '\n';
        $origin = qtype_preg_fa_transition::ORIGIN_TRANSITION_SECOND;

        $firstautomata = new qtype_preg_nfa(0, 0, 0, array());
        $firstautomata->read_fa($dotdescription1);
        $secondautomata = new qtype_preg_nfa(0, 0, 0, array());
        $secondautomata->read_fa($dotdescription2, $origin);
        $resultautomata = new qtype_preg_nfa(0, 0, 0, array());

        $resultautomata = $firstautomata->intersect_fa($secondautomata, 5, 1);
        $result = $resultautomata->write_fa();
        $dotresult = str_replace($search, $replace, $dotresult);
        $this->assertEquals($dotresult, $result, 'Result automata is not equal to expected');
    }
}